package com.runda.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;

import com.bbjob.dto.Result;
import com.bbjob.mapper.ActivateCodeMapper;
import com.bbjob.mapper.CenterMacSigninMapper;
import com.bbjob.mapper.SysOrganizationMapper;
import com.bbjob.model.ActivateCode;
import com.bbjob.model.CenterMacSignin;
import com.bbjob.model.SysOrganization;
import com.runda.query.QueryRegisteInfo;
import com.runda.service.IRegisteService;

@Service
public class RegisteServiceImpl implements IRegisteService {

	@Resource
	private SysOrganizationMapper sysOrganizationMapper;
	
	@Resource
	private ActivateCodeMapper activateCodeMapper;
	
	@Resource
	private CenterMacSigninMapper centerMacSigninMapper;
	
	@Override
	public Result registe(QueryRegisteInfo registeInfo) {
		
		if (StringUtils.isBlank(registeInfo.getUniqueCode())) {
			
			return new Result(8102, "学校唯一编码为空");
		}
		
		Example signinExample = new Example(CenterMacSignin.class);
		signinExample.createCriteria().andEqualTo("macAddress", registeInfo.getMacAddress());
		List<CenterMacSignin> signins = centerMacSigninMapper.selectByExample(signinExample);
		if (signins.size() > 0) {
			
			return new Result(200, "该机器MAC地址已经注册过");
		}
		
		
		Example organExample = new Example(SysOrganization.class);
		organExample.createCriteria().andEqualTo("uniqueCode", registeInfo.getUniqueCode());
		List<SysOrganization> organs = sysOrganizationMapper.selectByExample(organExample);
		
		if (organs.size() > 0) {
			SysOrganization organ = organs.get(0);
			
			List<ActivateCode> codes = activateCodeMapper.selectCodeByOrganId(organ.getId());
			
			if (codes.size() == 0) {
				return new Result(8103, "无可用授权码");
			} else {
				ActivateCode code = codes.get(0);
				
				CenterMacSignin centerMacSignin = new CenterMacSignin();
				centerMacSignin.setCreateTime(new Date());
				centerMacSignin.setAccodeId(code.getId());
				centerMacSignin.setMacAddress(registeInfo.getMacAddress());
				
				centerMacSigninMapper.insert(centerMacSignin);
				
				Integer nowTime = code.getNowTime() == null ? 0 : code.getNowTime();
				nowTime += 1;
				code.setNowTime(nowTime);
				code.setUpdateTime(new Date());
				activateCodeMapper.updateByPrimaryKeySelective(code);
				
				return new Result(200, "保存成功");
			}
		} else {
			return new Result(8102, "未查询到学校信息");
		}
	}

	@Override
	public Result validate(QueryRegisteInfo registeInfo) {
		Example example = new Example(CenterMacSignin.class);
		example.createCriteria().andEqualTo("macAddress", registeInfo.getMacAddress());
		
		List<CenterMacSignin> signins = centerMacSigninMapper.selectByExample(example);
		
		if (signins.size() > 0) {
			
			return new Result(200, "该机器已注册");
		} else {
			
			return new Result(8106, "该机器未注册");
		}
	}
}
